﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using QLNet;
using FinPlusCompCore;
using FinPlusBaseCore;
using FinPlusInterfaces;
using FinPlusAssembler;
using p = FinPlusCompQuant.QLConvParser;

namespace FinPlusCompQuant
{
    public class Index : FinPlusComponent
    {
       public string Name { get; private set; }
       public IborIndex IborIndex { get; private set; }

       //construct
       public Index(Market market, string curveName, string name, string indexType, string tenor, string fixings = "")
       {

            Name = name;
            market.SetIndex(name, Build(indexType, tenor, market, curveName));
    
            if(fixings != "") 
		    {
                var fixs = (object[,])fixings.ToArray2d();
		        //for each(f in fixs[1])
			    //    IborIndex.addFixing(Date(stoi(f[0])), stod(f[1]));
                //TODO
		    }
       }

       private IborIndex Build(string indexType, string tenor, Market market, string curveName)
       {
           try
           {
               var yeildTermStructure = market.GetLinkedCurve(curveName);

               switch (string.Format("{0}{1}", indexType.ToLower(), tenor.ToLower()))
               {
                   case "euribor1m": return new Euribor1M(yeildTermStructure);
                   case "euribor3m": return new Euribor3M(yeildTermStructure);
                   case "euribor6m": return new Euribor6M(yeildTermStructure);
                   case "euribor1y": return new Euribor1Y(yeildTermStructure);
                   case "usdlibor1m": return new USDLibor(p.GetPeriod(tenor), yeildTermStructure);
                   case "usdlibor3m": return new USDLibor(p.GetPeriod(tenor), yeildTermStructure);
                   case "usdlibor6m": return new USDLibor(p.GetPeriod(tenor), yeildTermStructure);
                   case "usdlibor1y": return new USDLibor(p.GetPeriod(tenor), yeildTermStructure);
                   default: throw new AnalyticsFormatException(string.Format("index {}{} not found", indexType, tenor));
               }
           }
           catch (Exception e)
           {
               throw new AnalyticsException(string.Format("indexType {0} not recognised", indexType), e);
           }
       }
    }
}
